var app = getApp(); 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message:'',
    jsonresult:'',
    user:{},
    answer:'',
    userarray:[],
    userId:0,
    reply:[],
    reply_length:0,
    radio:'',
    topicId:'',
  },

  radioChange1: function (e) {
    this.setData({
      radio: e.detail.value
    })
  },

  radioChange2: function (e) {
    this.setData({
      answer: e.detail.value
    })
  },

  answerInput: function (e) {
    this.setData({
      answer: e.detail.value
    })
  },

  
  onLoad: function (options) {
    console.log('onLoad')
    this.setData({
      topicId: options.topicId,
      user: app.globalData.user
    })
    //调用应用实例的方法获取全局数据
    this.getReply();
    this.getAllApprovers()
  },
  getReply: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/reply_getAllReplyByTopicId.action',
      data: {
        "topic.topicId": this.data.topicId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            reply: jsonresult.reply,
            reply_length: jsonresult.reply.length
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })

  },

  getAllApprovers: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/user_getAllApproversExceptSelf.action',
      data: {
        "user.userId": this.data.user.userId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            userarray: jsonresult.users,
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  submit:function(e){
    if (this.data.answer != "" & this.data.topicId != ""){
      console.log(this.data.userId)
      console.log(this.data.typeId)
      var that = this
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/approve_addApprove.action', 
        data: {
          'approve.answer': this.data.answer,
          'approve.user.userId': this.data.userarray[this.data.userId].userId,
          'approve.topic.topicId': this.data.topicId,
        },
        header: { 
          "content-type": "application/x-www-form-urlencoded"
        }, 
        method: "POST",
        success: function (res) {
          let jsonresult =JSON.parse(res.data)
          console.log(jsonresult.result)
          if (jsonresult.result == "error"){
             that.setData({
               message: jsonresult.msg 
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '提交成功,是否返回？',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 2
                  })
                } else {

                }
              }
            })
          }
        },
        fail: function (err) {
          console.log(err)
        }
      })
    }
  },

  bindUserChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      userId: e.detail.value,
      pickuserId: e.currentTarget.dataset.pickuserId
    })
  },
})